package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.domain.AjaxResult;
import com.itheima.common.domain.PageInfo;
import com.itheima.entity.PmsBrand;
import com.itheima.service.IPmsBrandService;
import com.itheima.vo.ChangeStatus;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/brand")
public class PmsBrandController {

    @Autowired
    private IPmsBrandService iPmsBrandService;

    /**
     * 添加品牌
     * @param pmsBrand
     * @return
     */
    @PostMapping("/create")
    public AjaxResult addBrand(@RequestBody PmsBrand pmsBrand){
        iPmsBrandService.save(pmsBrand);

        return AjaxResult.success("添加成功！",null);
    }

    /**
     * 批量删除品牌
     * @param ids
     * @return
     */
    @PostMapping("/delete/batch")
    public AjaxResult deleteIds(@RequestBody ArrayList<Integer> ids){
        iPmsBrandService.removeByIds(ids);
        return AjaxResult.success("批量删除成功！",null);
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @GetMapping("/delete/{id}")
    public AjaxResult deleteById(@PathVariable Integer id){
        iPmsBrandService.removeById(id);
        return AjaxResult.success("删除成功！",null);
    }

    /**
     * 获取全部品牌列表
     * @return
     */
    @GetMapping("/listAll")
    public AjaxResult listAllBrand(){
        List<PmsBrand> brandList = iPmsBrandService.list();
        return AjaxResult.success("展示成功!",brandList);
    }

    /**
     * 批量修改品牌状态
     * @param changeStatus
     * @return
     */
    @PostMapping("/update/factoryStatus")
    public AjaxResult updateFactoryStatus(@RequestBody ChangeStatus changeStatus){
        ArrayList<Integer> ids = changeStatus.getIds();
        for (Integer id : ids) {
            PmsBrand brand = iPmsBrandService.getById(id);
            brand.setFactoryStatus(changeStatus.getFactoryStatus());
            iPmsBrandService.updateById(brand);
        }
        return AjaxResult.success("批量修改品牌状态成功",null);
    }

    /**
     * 批量更新显示状态
     * @param changeStatus
     * @return
     */
    @PostMapping("/update/showStatus")
    public AjaxResult updateShowStatus(@RequestBody ChangeStatus changeStatus){
        ArrayList<Integer> ids = changeStatus.getIds();
        for (Integer id : ids) {
            PmsBrand brand = iPmsBrandService.getById(id);
            brand.setFactoryStatus(changeStatus.getFactoryStatus());
            iPmsBrandService.updateById(brand);
        }
        return AjaxResult.success("批量更新显示状态成功",null);
    }

    /**
     * 更新品牌
     * @param id
     * @param pmsBrand
     * @return
     */
    @PostMapping("/update/{id}")
    public AjaxResult updateById(@PathVariable Long id , @RequestBody PmsBrand pmsBrand){
        pmsBrand.setId(id);
        iPmsBrandService.updateById(pmsBrand);
        return AjaxResult.success("更新品牌成功!",null);

    }

    /**
     * 根据编号查询品牌信息
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public AjaxResult selectById(@PathVariable Long id){
        PmsBrand brand = iPmsBrandService.getById(id);
        return AjaxResult.success("查询成功",brand);
    }


    @GetMapping("/list")
    public AjaxResult listBrand(String keyword,Integer pageNum,Integer pageSize){
        //构造分页构造器
        Page<PmsBrand> pmsBrandPage = new Page<>(pageNum, pageSize);
        //构造条件构造器
        LambdaQueryWrapper<PmsBrand> pmsBrandLambdaQueryWrapper = new LambdaQueryWrapper<>();
        pmsBrandLambdaQueryWrapper.like(StringUtils.isNotEmpty(keyword),PmsBrand::getName,keyword);
        //查询
        iPmsBrandService.page(pmsBrandPage,pmsBrandLambdaQueryWrapper);

        PageInfo pageList = PageInfo.toPageList(pmsBrandPage);

        return AjaxResult.success("分页查询成功!",pageList);
    }
}
